<!DOCTYPE html>
<html>

<head>
    <title>KDT - Delivery system</title>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width,initial-scale=1.0,maximum-scale=1.0,user-scalable=no">
    <script src="/kdt.js"></script>
    <script type="text/javascript">
    function register_me() {
        var phone = document.getElementById('form_phone').value
        var password = document.getElementById('form_password').value
        var repeat = document.getElementById('form_repeat').value
        if (phone.length < 8) {
            alert('phone number incorrect')
            return
        }
        if (password.length < 6) {
            alert('password too short')
            return
        }
        if (password != repeat) {
            alert('password mismatch')
            return
        }
        var xhr = new XMLHttpRequest()
        xhr.onload = function(e) {
            if (this.status == 200 || this.status == 304) {
                var o = JSON.parse(xhr.responseText)
                if (o.State == 'OK') {
                    setCookie('KDT-Admin-Sid', o.Info, 365)
                    setCookie("KDT-Mode", "Admin", 365)
                    setCookie('KDT-Admin-Phone', phone, 365)
                    location.href = '/'
                } else {
                    alert(xhr.responseText)
                }
            }
        }
        xhr.open("POST", "/apis/admin/register", true)
        var obj = new Object()
        obj.Phone = document.getElementById("form_phone").value
        obj.Password = document.getElementById("form_password").value
        // document.getElementById('result').innerHTML=JSON.stringify(obj)
        xhr.send(JSON.stringify(obj))
    }
    </script>
</head>

<body style="display: flex;flex-direction: column;align-items: center;">
    <h1 style="display: inline;">KDT  Delivery System</h1>
    <br>
    <div>
        <span><b>Admin register</b></span>
        <br>
        <br>
        <input type="number" name="phone" id="form_phone" placeholder="phone number" maxlength="12">
        <br>
        <br>
        <input type="password" name="password" placeholder="password" id="form_password" maxlength="15">
        <br>
        <br>
        <input type="password" placeholder="repeat password" id="form_repeat" maxlength="15" onkeyup="if(event.keyCode==13){register_me()}">
        <br>
        <br>
        <input type="button" value="Admin register" name="register" onclick="register_me()">
        <a href="/adminLogin.html">Admin Login</a>
        <br>
        <a href="/userLogin.html">User Login</a>
        <br>
        <div id="result"></div>
    </div>
</body>

</html>